package com.ks.api.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ks.api.entity.CultivationLevel;
import com.ks.api.mapper.CultivationLevelMapper;
import com.ks.api.service.CultivationLevelService;

@Service
public class CultivationLevelServiceImpl implements CultivationLevelService {

    @Autowired
    private CultivationLevelMapper cultivationLevelMapper;
    
    @Override
    public List<CultivationLevel> findAllLevels() {
        return cultivationLevelMapper.findAll();
    }

    @Override
    public CultivationLevel findLevelById(Long id) {
        return cultivationLevelMapper.findById(id);
    }

    @Override
    public CultivationLevel findLevelByScore(Integer score) {
        return cultivationLevelMapper.findByScore(score);
    }

    @Override
    public CultivationLevel findLevelByStages(String majorStage, String minorStage, String microStage) {
        return cultivationLevelMapper.findByStages(majorStage, minorStage, microStage);
    }

    @Override
    public List<CultivationLevel> findLevelsByMajorStage(String majorStage) {
        return cultivationLevelMapper.findByMajorStage(majorStage);
    }

    @Override
    public boolean addLevel(CultivationLevel level) {
        return cultivationLevelMapper.insert(level) > 0;
    }

    @Override
    public boolean updateLevel(CultivationLevel level) {
        return cultivationLevelMapper.update(level) > 0;
    }

    @Override
    public boolean deleteLevelById(Long id) {
        return cultivationLevelMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean deleteAllLevels() {
        return cultivationLevelMapper.deleteAll() > 0;
    }

    @Override
    public CultivationLevel getCurrentLevel(Integer score) {
        List<CultivationLevel> allLevels = cultivationLevelMapper.findAll();
        
        CultivationLevel currentLevel = null;
        
        for (CultivationLevel level : allLevels) {
            if (level.getScore() <= score) {
                currentLevel = level;
            } else {
                break;
            }
        }
        
        return currentLevel;
    }

    @Override
    public CultivationLevel getNextLevel(Integer currentScore) {
        List<CultivationLevel> allLevels = cultivationLevelMapper.findAll();
        
        for (int i = 0; i < allLevels.size(); i++) {
            if (allLevels.get(i).getScore() > currentScore) {
                return allLevels.get(i);
            }
        }
        
        return null; // 已经是最高等级
    }
} 